/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gem.gui.courses;

import gem.controller.DepartmentActionListener;
import gem.gui.courses.Common;
import gem.model.core.*;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

/**
 *
 * @author Dusan
 */
public class PopupMenu
{
    //-----------------------------------------------------------------------------------//
    private static JPopupMenu courseMenu = null;
    private static JPopupMenu scienceDomainMenu = null;
    private static JPopupMenu departmentMenu = null;
    private static JPopupMenu rootMenu = null;
    //-----------------------------------------------------------------------------------//   
    public static JPopupMenu getMenu( Object type, ActionListener a )
    {
        if( type instanceof Course )
        {
            return getCourseMenu( a );
        }
        else if( type instanceof ScienceDomain )
        {
            return getScienceDomainMenu( a );
        }
        else if( type instanceof Department )
        {
            return getDepartmentMenu( a );
        }
        else
        {
            return getRootMenu( a );
        }
    }
    //-----------------------------------------------------------------------------------//
    public static JPopupMenu getMenu( DBEntity sender, DepartmentActionListener dal )
    {
        switch( sender.getType() )
        {
        case COURSE:
            return getCourseMenu(sender, dal);
        case DEPARTMENT:
            return getDepartmentMenu(sender, dal);
        case SCIENCEDOMAIN:
            return getScienceDomainMenu(sender, dal);
        case NONE:
            return getRootMenu(sender, dal);
        }
        return null;
    }
    //-----------------------------------------------------------------------------------//
    private static JPopupMenu getCourseMenu( final DBEntity sender, final DepartmentActionListener dal )
    {
        if( courseMenu == null )
        {
            courseMenu = new JPopupMenu("Kursevi");
            JMenuItem mi = new JMenuItem("Izmeni ime");
            mi.setActionCommand(Common.COMMAND_RENAME);
            courseMenu.add(mi);
            mi = new JMenuItem("Obriši kurs");
            mi.setActionCommand(Common.COMMAND_REMOVE);
            courseMenu.add(mi);
            
            for( final Component m : courseMenu.getComponents() )
            {
                if( m instanceof JMenuItem )
                {
                    ((JMenuItem)m).addActionListener( new ActionListener()
                    {
                        public void actionPerformed(ActionEvent e)
                        {                            
                            dal.departmentActionPerformed( sender, ((JMenuItem)m).getActionCommand() );
                        }
                    });
                }
            }
        }
        return courseMenu;
    }
    //-----------------------------------------------------------------------------------//
    private static JPopupMenu getScienceDomainMenu( final DBEntity sender, final DepartmentActionListener dal )
    {
        if( scienceDomainMenu == null )
        {
            scienceDomainMenu = new JPopupMenu("Naucna oblast");
            JMenuItem mi = new JMenuItem("Izmeni ime");
            mi.setActionCommand(Common.COMMAND_RENAME);
            scienceDomainMenu.add(mi);
            mi = new JMenuItem("Obriši oblast");
            mi.setActionCommand( Common.COMMAND_REMOVE );
            scienceDomainMenu.add(mi);
            mi = new JMenuItem("Dodaj kurs");
            mi.setActionCommand(Common.COMMAND_ADD);
            scienceDomainMenu.add(mi);

            for( final Component m : scienceDomainMenu.getComponents())
            {
                if( m instanceof JMenuItem )
                {
                    ((JMenuItem)m).addActionListener( new ActionListener() {

                        public void actionPerformed(ActionEvent e)
                        {
                            dal.departmentActionPerformed(sender, ((JMenuItem)m).getActionCommand());
                        }
                    });
                }
            }
        }

        return scienceDomainMenu;
    }
    //-----------------------------------------------------------------------------------//
    private static JPopupMenu getDepartmentMenu( final DBEntity sender, final DepartmentActionListener dal )
    {
        if( departmentMenu == null )
        {
            departmentMenu = new JPopupMenu("Katedre");
            JMenuItem mi = new JMenuItem("Izmeni ime");
            mi.setActionCommand(Common.COMMAND_RENAME);
            departmentMenu.add(mi);
            mi = new JMenuItem("Obriši katedru");
            mi.setActionCommand(Common.COMMAND_REMOVE);
            departmentMenu.add(mi);
            mi = new JMenuItem("Dodaj oblast");
            mi.setActionCommand(Common.COMMAND_ADD);
            departmentMenu.add(mi);

            for( final Component m : departmentMenu.getComponents())
            {
                if( m instanceof JMenuItem )
                {
                    ((JMenuItem)m).addActionListener(new ActionListener() {

                        public void actionPerformed(ActionEvent e)
                        {
                            dal.departmentActionPerformed(sender, ((JMenuItem)m).getActionCommand());
                        }
                    });
                }
            }
        }

        return departmentMenu;
    }
    //-----------------------------------------------------------------------------------//
    private static JPopupMenu getRootMenu( final DBEntity sender, final DepartmentActionListener dal )
    {
        if( rootMenu == null )
        {
            rootMenu = new JPopupMenu("Glavni meni");
            final JMenuItem mi = new JMenuItem("Dodaj katedru");
            mi.setActionCommand(Common.COMMAND_ADD);
            mi.addActionListener( new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    dal.departmentActionPerformed(sender, mi.getActionCommand());
                }
            } );
            rootMenu.add( mi );
        }
        return rootMenu;
    }
    //-----------------------------------------------------------------------------------//
    //-----------------------------------------------------------------------------------//
    private static JPopupMenu getDepartmentMenu( ActionListener a )
    {
        return departmentMenu;
    }

    private static JPopupMenu getRootMenu( ActionListener a )
    {      
        return rootMenu;
    }
    private static JPopupMenu getScienceDomainMenu( ActionListener a )
    {
        return null;
    }
    private static JPopupMenu getCourseMenu( ActionListener a )
    {
        return null;
    }
}
